package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
/*
请求URL：http://localhost:8080 /customer/list
请求参数：Integer page, Integer rows, String  customerName
请求方式：POST
返回值类型：JSON
返回值：Map<String,Object>
 */

    @PostMapping("list")
    public Map<String,Object> list(Integer page, Integer rows, String  customerName){
        page=(page-1)*rows;
        List<Customer> list =customerService.getCustomerList(page,rows,customerName);
        Map<String, Object> map = new HashMap<>();
        map.put("total",list.size());
        map.put("rows",list);
        return map;
    }

    /*
    请求URL：http://localhost:8080/ customer/save?customerId=1
    请求参数：Customer customer
    请求方式：POST
    返回值类型：JSON
    返回值：ServiceVO
     */
    //添加或修改
    @PostMapping("save")
    public ServiceVO save(Customer customer){
        return customerService.saveOrUpdate(customer);
    }

    /*
    请求URL：http://localhost:8080/customer/delete
    请求参数：String  ids
    请求方式：POST
    返回值类型：JSON
    返回值：ServiceVO
     */
    @PostMapping("delete")
    public ServiceVO delete(String  ids){
        return customerService.delete(ids);
    }

    /*
    请求URL：http://localhost:8080/customer/getComboboxList
    请求参数：String q（客户名称）
    请求方式：POST
    返回值类型：JSON
    返回值：List<Customer>
     */
    //客户下拉列表
    @PostMapping("getComboboxList")
    public List<Customer> getComboboxList(String customerName){
        List<Customer> list=customerService.getComboboxList(customerName);
        return list;
    }
}
